<?php
class Login extends CI_Controller
{

    function __construct()
    {
        parent::__construct();
        $this->load->model('us_model');
        $this->load->library('form_validation');
        $this->load->library('gf');
    }

    function index()
    {
        $a = $this->session->userdata('logged_in');
        if($a=='')
            $this->login();
        else
            redirect(base_url() . 'cirugias/', 'refresh');
            
    }
    
    function login()
    {
        $data['title'] = 'Loguearse';
        $data['view'] = 'login/login_form';
        $this->load->view('templates/temp_simple', $data);
    }

    function verificar()
    {
        //This method will have the credentials validation
       
        $this->form_validation->set_rules('u_nick', 'Usuario', 'trim|required|xss_clean');
        $this->form_validation->set_rules('u_clave', 'Clave', 'trim|required|xss_clean|callback_verificar_bd');
        if ($this->form_validation->run() == FALSE)
        {
            //Field validation failed.&nbsp; User redirected to login page
            $data['title'] = 'Loguearse';
            $data['view'] = 'login/login_form';
            $this->load->view('templates/temp_simple', $data);
        }
        else
        {
            //Go to private area
            redirect(base_url() . 'cirugias/', 'refresh');
        }
    }

    function verificar_bd($u_clave)
    {
        $u_nick = $this->input->post('u_nick');

        //query the database
        $row = $this->us_model->login($u_nick, $u_clave);

        if ($row)
        {
            $sess_array = array(
                'id_usuario' => $row->id_usuario,
                'u_nick' => $row->u_nick,
                'u_clave' => $row->u_clave,
                'u_rol' => $row->u_rol,
                'u_nombre' => $row->u_nombre
            );
            $this->session->set_userdata('logged_in', $sess_array);

            return TRUE;
        }
        else
        {
            
            $this->form_validation->set_message('verificar_bd', 'Usuario o clave incorrecta');
            return false;
        }
    }
    
    function salir()
    {
        $this->session->unset_userdata('logged_in');
        redirect(base_url()."login/", 'refresh');
    }
    
    function bd()
    {
        $query="mysqldump --host=localhost --user=ye000454_chango --password=InstitutoDrMercado2013 ye000454_siscat> chango.sql";
        $this->db->query($query);
    }
}
?>
